import { Request, Response } from "express";
import { UserService } from "../services/user.service";

export class UserController {
  private userService: UserService;

  constructor() {
    this.userService = new UserService();
  }
  /**
   * /users
   */
  getAllUsers = async (req: Request, res: Response) => {
    let users = await this.userService.getAllUsers();
    // users = users.map((item: any) => {
    //   const { password, ...userWithoutPassword } = item;
    //   return userWithoutPassword;
    // });

    return res.status(200).json({
      code: 200,
      success: true,
      message: "successfully",
      data: users,
    });
  };
  usersForPage = async (req: Request, res: Response) => {
    // 取出 page, pageSize, keyword, 可能为空
    const page = parseInt(req.query.page as string) || 1;
    const pageSize = parseInt(req.query.pageSize as string) || 5;
    const keyword = (req.query.search as string) || null;
    const data = await this.userService.usersForPage(page, pageSize, keyword);

    return res.status(200).json({
      code: 200,
      success: true,
      message: "successfully",
      data: {
        data: data.users,
        total: data.total,
        page: page,
        pageSize: pageSize,
      },
    });
  };
  getUserById = async (req: Request, res: Response) => {
    const { id } = req.params;
    const user = await this.userService.getUserById(id);
    if (!user) {
      return res.status(404).json({
        code: 404,
        success: false,
        message: "user not found",
        data: null,
      });
    }
    const { password, ...userWithoutPassword } = user;
    return res.status(200).json({
      code: 200,
      success: true,
      message: "successfully",
      data: userWithoutPassword,
    });
  };
  getUserByIdForProfile = async (req: Request, res: Response) => {
    const { id } = req.params;
    const user = await this.userService.getUserByIdForProfile(id);
    if (!user) {
      return res.status(404).json({
        code: 404,
        success: false,
        message: "user not found",
        data: null,
      });
    }
    const { password, ...userWithoutPassword } = user;
    return res.status(200).json({
      code: 200,
      success: true,
      message: "successfully",
      data: userWithoutPassword,
    });
  };
  updateUser = async (req: Request, res: Response) => {
    const { id } = req.params;
    const { email, password, bio, avatar, role } = req.body;
    const user = await this.userService.getUserById(id);
    if (!user) {
      return res.status(404).json({
        code: 404,
        success: false,
        message: "user not found",
        data: null,
      });
    }
    let updatedInput = user;
    // 过滤掉空值
    Object.assign(
      updatedInput,
      ...Object.entries({ email, password, bio, avatar, role })
        .filter(([_, v]) => v != null || v != undefined)
        .map(([k, v]) => ({ [k]: v }))
    );
    // 排除id, blog_count
    delete updatedInput.id;
    delete updatedInput.blog_count;
    const updatedUser = await this.userService.updateUser(id, updatedInput);
    const { password: updatedPassword, ...userWithoutPassword } = updatedUser;
    return res.status(200).json({
      code: 200,
      success: true,
      message: "successfully",
      data: userWithoutPassword,
    });
  };
  deleteUser = async (req: Request, res: Response) => {
    const { id } = req.params;
    const user = await this.userService.getUserById(id);
    if (!user) {
      return res.status(404).json({
        code: 404,
        success: false,
        message: "user not found",
        data: null,
      });
    }
    await this.userService.deleteUser(id);
    return res.status(200).json({
      code: 200,
      success: true,
      message: "successfully",
      data: null,
    });
  };
}
